package mn.slide.dailynews.tv;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import mn.slide.dailynews.R;
import mn.slide.dailynews.helper.DatabaseHelper;
import mn.slide.dailynews.model.Tv;
import mn.slide.dailynews.model.TvDay;
import mn.slide.dailynews.model.TvDayItem;
import mn.slide.dailynews.tv.adapter.ItemListAdapter;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.j256.ormlite.android.apptools.OpenHelperManager;

public class TvActivity extends Activity implements OnClickListener
{
	private static final String	TAG							= TvActivity.class.getName();
	private static final int	SWIPE_MIN_DISTANCE			= 120;
	private static final int	SWIPE_MAX_OFF_PATH			= 250;
	private static final int	SWIPE_THRESHOLD_VELOCITY	= 200;
	private ViewFlipper			channelFlipper;
	private ViewFlipper			dayFlipper;
	private LayoutInflater		inflater;

	private DatabaseHelper		databaseHelper				= null;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tv);
		inflater = getLayoutInflater();
		channelFlipper = (ViewFlipper) findViewById(R.id.tvChannelFlipper);
		final GestureDetector gestureDetectorChannel = new GestureDetector(new MyGestureDetector(this, channelFlipper));
		View.OnTouchListener gestureListenerChannel = new View.OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				return gestureDetectorChannel.onTouchEvent(event);
			}
		};
		channelFlipper.setOnClickListener(TvActivity.this);
		channelFlipper.setOnTouchListener(gestureListenerChannel);

		dayFlipper = (ViewFlipper) findViewById(R.id.tvDayFlipper);
		final GestureDetector gestureDetectorDay = new GestureDetector(new MyGestureDetector(this, dayFlipper));
		View.OnTouchListener gestureListenerDay = new View.OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				return gestureDetectorDay.onTouchEvent(event);
			}
		};
		dayFlipper.setOnClickListener(TvActivity.this);
		dayFlipper.setOnTouchListener(gestureListenerDay);

		initChannelFlipper();
		initDayFlipper();

	}

	private void initChannelFlipper()
	{
		try
		{
			List<Tv> tvs = getHelper().getTvDao().queryForAll();
			for (Tv tv : tvs)
			{
				View view = inflater.inflate(R.layout.tvlist_row, null);
				ImageView imageView = (ImageView) view.findViewById(R.id.tvImage);
				Bitmap bm = BitmapFactory.decodeByteArray(tv.getImage(), 0, tv.getImage().length);
				imageView.setImageBitmap(bm);
				view.setTag(tv);
				channelFlipper.addView(view);
			}
			for (Tv tv : tvs)
			{
				View view = inflater.inflate(R.layout.tvlist_row, null);
				ImageView imageView = (ImageView) view.findViewById(R.id.tvImage);
				Bitmap bm = BitmapFactory.decodeByteArray(tv.getImage(), 0, tv.getImage().length);
				imageView.setImageBitmap(bm);
				view.setTag(tv);
				channelFlipper.addView(view);
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	private void initDayFlipper()
	{
		Tv tv = (Tv) channelFlipper.getCurrentView().getTag();
		for (TvDay tvDay : tv.tvDays)
		{
			ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.tv_day_viewflipper_layout, null);
			TextView tvDayNameTextView = (TextView) layout.findViewById(R.id.tvDayName);
			tvDayNameTextView.setText(tvDay.name);
			ListView itemListView = (ListView) layout.findViewById(R.id.tvDayItemListView);
			List<TvDayItem> items = new ArrayList<TvDayItem>();
			for (TvDayItem item : tvDay.tvDayItems)
				items.add(item);
			ItemListAdapter adapter = new ItemListAdapter(this, R.layout.tv_day_viewflipper_layout_list_row, items);
			itemListView.setAdapter(adapter);
			dayFlipper.addView(layout);
		}
	}

	private void flipperNext(ViewFlipper flipper)
	{
		Log.d(TAG, "next");
		flipper.setInAnimation(this, R.anim.slide_right_in);
		flipper.setOutAnimation(this, R.anim.slide_right_out);
		flipper.showNext();
		if ( flipper == channelFlipper )
			initDayFlipper();
	}

	private void flipperPrevious(ViewFlipper flipper)
	{
		Log.d(TAG, "prev");
		flipper.setInAnimation(this, R.anim.slide_left_out);
		flipper.setOutAnimation(this, R.anim.slide_left_in);
		flipper.showPrevious();
		if ( flipper == channelFlipper )
			initDayFlipper();
	}

	private DatabaseHelper getHelper()
	{
		if ( databaseHelper == null )
			databaseHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
		return databaseHelper;
	}

	private class MyGestureDetector extends SimpleOnGestureListener
	{
		TvActivity	activity;
		ViewFlipper	flipper;

		public MyGestureDetector(TvActivity activity, ViewFlipper flipper)
		{
			this.activity = activity;
			this.flipper = flipper;
		}

		private void swipeFromRightToLeft()
		{
			activity.flipperNext(flipper);
		}

		private void swipeFromLeftToRight()
		{
			activity.flipperPrevious(flipper);
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
		{
			if ( Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH )
				return false;
			if ( e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY )
			{
				swipeFromLeftToRight();
			}
			else if ( e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY )
			{
				swipeFromRightToLeft();
			}
			return false;
		}
	}
	public void onClick(View v)
	{
		
	}
}